//
//  Register2ViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/10/24.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit

class Register2ViewController: BaseUIViewController {
    
    let nameField = LoginTextField()
    let companyField = LoginTextField()
    let jobField = LoginTextField()
    
    let model: LoginModel
    let registerModel: RegisterModel
    
    init(model: LoginModel, registerModel: RegisterModel) {
        self.model = model
        self.registerModel = registerModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
    }
    
    func setupView() {
        self.navigationController?.navigationItem.title = "个人信息".localized
        
        let backgroundImage = UIImageView()
        backgroundImage.config
            .imageName("login_background")
            .addTo(view)
            .makeConstraints { (make) in
                make.left.top.right.equalTo(0)
                make.height.equalTo(backgroundImage.snp.width).multipliedBy(0.69)
        }
        
        let foregroundView = UIView().config
            .backgroundColor(.white)
            .cornerRadius(5)
            .shadowColor(.black)
            .shadowRadius(10)
            .shadowOffset(.zero)
            .shadowOpacity(0.1)
            .addTo(view)
            .makeConstraints { (make) in
                make.left.equalTo(15)
                make.centerX.equalToSuperview()
                make.top.equalTo(backgroundImage.snp.bottom).offset(-70)
        }.view
        
        let titleLabel = UILabel().config
            .font(UIFont.systemFont(ofSize: 14, weight: .medium))
            .textColor(#colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1))
            .text("完善个人信息，赢取100积分奖励".localized)
            .addTo(foregroundView)
            .makeConstraints { (make) in
                make.top.equalTo(0)
                make.height.equalTo(60)
                make.centerX.equalToSuperview()
        }.view
        
        nameField.config
            .image(AppIcon.image("", size: 20, color: #colorLiteral(red: 0.8274509804, green: 0.8274509804, blue: 0.8274509804, alpha: 1)))
            .placeHolder("请输入姓名".localized)
            .addTo(foregroundView)
            .makeConstraints { (make) in
                make.left.equalTo(20)
                make.right.equalTo(-20)
                make.top.equalTo(titleLabel.snp.bottom)
                make.height.equalTo(45)
        }
        
        companyField.config
            .image(AppIcon.image("", size: 20, color: #colorLiteral(red: 0.8274509804, green: 0.8274509804, blue: 0.8274509804, alpha: 1)))
            .placeHolder("请输入公司名称".localized)
            .extra{
                $0.optional = true
        }.addTo(foregroundView)
            .makeConstraints { (make) in
                make.left.equalTo(20)
                make.right.equalTo(-20)
                make.top.equalTo(nameField.snp.bottom).offset(10)
                make.height.equalTo(45)
        }
        
        jobField.config
            .image(AppIcon.image("", size: 20, color: #colorLiteral(red: 0.8274509804, green: 0.8274509804, blue: 0.8274509804, alpha: 1)))
            .placeHolder("请输入职位".localized)
            .extra{
                $0.optional = true
        }.addTo(foregroundView)
            .makeConstraints { (make) in
                make.left.equalTo(20)
                make.right.equalTo(-20)
                make.top.equalTo(companyField.snp.bottom).offset(10)
                make.height.equalTo(45)
        }
        
        let linkLabel = UILabel().config
            .fontSize(12)
            .textColor(#colorLiteral(red: 0.6941176471, green: 0.6941176471, blue: 0.6941176471, alpha: 1))
            .numberOfLines(0)
            .isUserInteractionEnabled(true)
            .addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(showUrl)))
            .addTo(foregroundView)
            .makeConstraints { (make) in
                make.centerX.equalTo(self.view.snp.centerX)
                make.bottom.equalTo(-15)
                make.left.greaterThanOrEqualTo(30)
        }.view
        
        let linkText = "用户协议和隐私政策".localized
        let text = NSMutableAttributedString.init(string: "点击登录按钮代表您已同意".localized +  "  " + linkText)
        text.setTextColor(UIColor.init(red: 78.0 / 255.0, green: 123.0 / 255.0, blue: 179.0 / 255.0, alpha: 1), text: linkText)
        linkLabel.attributedText = text
        UIButton().config
            .backgroundImage(UIImage.imageWithColor(APPConfig.baseColor))
            .clipsToBounds(true)
            .titleColor(.white)
            .cornerRadius(5)
            .addTarget(self, action: #selector(finishButtonClick))
            .title("完成".localized)
            .font(UIFont.systemFont(ofSize: 16, weight: .medium))
            .addTo(foregroundView)
            .makeConstraints { (make) in
                make.top.equalTo(jobField.snp.bottom).offset(50)
                make.bottom.equalTo(linkLabel.snp.top).offset(-15)
                make.left.equalTo(20)
                make.height.equalTo(50)
                make.centerX.equalToSuperview()
        }
    }
    
    @objc func showUrl() {
        let vc = BaseWebViewController.init(url: APPConfig.protocolUrl)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func finishButtonClick() {
        for i in [nameField] {
            if i.text.count == 0 {
                APPHud.shared.showError(status: i.placeHolder)
                return
            }
        }
        ApiManager.updateUserInfo(model: self.model, email: registerModel.email, name: nameField.text, phone: registerModel.phone, country: registerModel.country, company: companyField.text, job: jobField.text, companyWebsite: "", remarks: "")
            .request()
            .filterCode200()
            .trackingLoading()
            .subscribe(onNext: { [weak self] (_) in
                guard let self = self else { return }
                APPUser.shared.state = .login(self.model)
                AppCache.shared.updateObject(for: APPUserDefault.self) { (userDefault) in
                    userDefault.lastLoginEmail = self.registerModel.email
                }
                UIApplication.shared.keyWindow?
                    .setRootViewController(MainTabBarViewController(), type: .push)
            }).disposed(by: rx.disposeBag)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.view.endEditing(true)
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .clearColor
    }
}
